<template>
  <div id="appo">
    <!-- 头像图片 -->
    <div class="avatar_box">
      <img src="@/assets/logo.b5a855ee.png" alt="" />
    </div>

    <!-- 卡片组件 -->
    <el-card class="login-card">
      <!-- 登录表单 -->
      <el-form
        ref="loginForm"
        :rules="loginRules"
        :model="loginForm"
        class="elform"
      >
        <el-form-item prop="username">
          <el-input v-model="loginForm.username">
            <i slot="prefix" class="el-input__icon el-icon-s-custom"></i>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="btninfo">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <router-view></router-view>
  </div>
</template>

<script>
import { mapActions } from "vuex";
export default {
  name: "",
  components: {},
  props: {},
  data() {
    return {
      loginRules: {
        username: [
          { required: true, message: "请填写用户名", trigger: "blur" },
          { min: 2, max: 6, message: "用户名长度为2-6", trigger: "blur" },
        ],
        password: [
          { required: true, message: "密码必填", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度为3-10个字符",
            trigger: "blur",
          },
        ],
      },
      loginForm: {
        username: "admin",
        password: "123456",
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  methods: {
    ...mapActions(["user/login"]),
    async login() {
      try {
        await this.$refs.loginForm.validate();
        // 只有在上面代码成功了之后才会执行下面的代码
        await this["user/login"](this.loginForm);
        this.$router.push("/layout");
      } catch (error) {
        console.log(error);
      }
    },
    btninfo() {
      this.$refs.loginForm.resetFields();
    },
  },
};
</script>

<style scoped>
#appo {
  background-color: #2b4b6b;

  position: relative;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-card {
  position: absolute;
  left: 50%;
  top: 389px;
  width: 500px;
  height: 300px;
  transform: translate(-50%, -50%);
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.el-form {
  margin-top: 90px !important;
}
.avatar_box {
  position: absolute;
  background-color: #fff;
  left: 50%;
  top: 235px;
  width: 130px;
  height: 130px;
  border-radius: 50%;
  padding: 10px;
  border: 1px solid #fff;
  transform: translate(-50%, -50%);
  box-shadow: 0 0 10px #ddd;
  z-index: 2;
}
.avatar_box img {
  width: 100%;
  height: 100%;
}
</style>
